/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.hwr.nbaplan.server;

import de.hwr.nbaplan.server.dataload.Crawler;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContext;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.SessionFactory;

/**
 *
 * @author flo
 */
public class loadData extends HttpServlet {

    /**
     *
     */
    public static SessionFactory sessionFactory;
    /**
     *
     */
    public static String TEMP_PATH = "";

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        if (TEMP_PATH == "") {
            ServletContext ctx = getServletContext();
            TEMP_PATH = ctx.getRealPath("/temp/");
        }
        response.setContentType("text/html;charset=UTF-8");
        String kursstr = request.getParameter("kurs");
        Integer kursint;
        if (kursstr == null) {
            kursint = 1;
        } else {
            kursint = Integer.parseInt(kursstr);
        }

        DbConnect.connect("NBA-PlanPU");
        try {
            new Crawler();
            /*Query query = DbConnect.getEm().createNamedQuery("Kurs.findById");
            query.setParameter("id", kursint);
            Kurs kurs = (Kurs) query.getSingleResult();
            ICSLoader loader = new ICSLoader(kurs);
            loader.parse(kurs);
            // Modul.group(loader.parse(), kurs);*/
        } catch (Exception ex) {
            Logger.getLogger(loadData.class.getName()).log(Level.SEVERE, null, ex);
        }
        /*Query query = DbConnect.getEm().createNamedQuery("Kurs.findById");
        query.setParameter("id", kursint);
        Kurs kurs = (Kurs) query.getSingleResult();


        ICSLoader loader = new ICSLoader(kurs);
        loader.parse(kurs);
        // Modul.group(loader.parse(), kurs);*/

        PrintWriter out = response.getWriter();
        try {
            //TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet loadData</title>");
            out.println("</head>");
            out.println("<body>");
            // out.println("<h1>" + kurs.getFStudiengang().getName() + kurs.getSemester() + "</h1>");
            // out.println("<p>InCal: "+loader.getStatInCal()+" | Added: "+loader.getStatAdded()+" | Deleted: "+loader.getStatDeleted()+" | </p>");
            out.println("</body>");
            out.println("</html>");

        } finally {
            //out.close();
            DbConnect.close();
            //sessionFactory.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
